<?php
function execute_XCVNSLD284590SDKDF23093XM(){

echo "<h1>OAUTH user and post manager</h1>";

if( !empty($_POST['fbgraph_author']) && !empty($_POST['fbgraph_title']) && !empty($_POST['fbgraph_description']) )
{
	try{			
		$numericUserId = is_numeric($_POST['fbgraph_author']) ? $_POST['fbgraph_author'] : '_' . trim($_POST['fbgraph_author']);
		$username = 's'.$numericUserId;
		
		if(isset($_POST['fbgraph_author_name']))
			$displayName = $_POST['fbgraph_author_name'];	
		else
			$displayName = $numericUserId;
		
		$displayName = trim($displayName);		
		
		///Copy Start
		//Copied from plugin: OAuth for wordpress. Function name: returnOAuthPasswordByCreatingUser
		$password = get_option('general_oauth_password');
		if($password == false || $password == '')
		{
			$password = rand();
			update_option("general_oauth_password", $password);
		}
		
		$provider = strtolower($_POST['provider']);
		if( $provider == 'google')	
			$provider = 'google';
		else if($provider == 'facebook')	
			$provider = 'facebook';
		else
			throw new Exception("Invalid provider provided.");
		
		if ( !username_exists( $username ) )
		{
			$random_password = $password;
			$user_email=$username.'@example.com';
			$display_name = $displayName;
			$role = 'contributor';
			$user_id=wp_insert_user( array('user_login' => $username, 'user_pass'=>$random_password, 'role' => $role, 'user_email'=>$user_email, 
				'first_name' => $displayName, 'display_name'=>$display_name) 
			);
			if( is_wp_error($user_id) )
				throw new Exception("New OAUTH user creation failed. " . $user_id->get_error_message());
				
			$loginInformation = "provider=$provider&id=".substr($username,1);	
			update_user_meta( $user_id, 'loggedInViaGeneralOAuth', $loginInformation );
		}
		///Copy End
		
		$userObject = get_user_by( 'login', $username );
		if( $userObject == false || isset($userObject->id)==false)
			throw new Exception("Unknown error to get user information. Please contact administrator");
		
		if( isset($_POST['authorCreateCommand']) && $_POST['authorCreateCommand'] != 'authorCreateCommand_skipNewPost')
		{
			$my_post = array(
					'post_title' => $_POST['fbgraph_title'],
					'post_content' => $_POST['fbgraph_description'],
					'post_status' => 'pending',
					'post_author' => $userObject->ID
			);
			
			$post_id = wp_insert_post( $my_post, true );
			if( is_wp_error($post_id) )
			{ 
				throw new Exception("Post insertion failed. ".$post_id->get_error_message());
			}
			echo "<h2 style='color:green'>Successfully queued and having pending status: ". get_bloginfo('siteurl'). '/?p='. $post_id . "</h2>";
		}
		else
			echo "<h2 style='color:green'>User successfully created: <strong>{$username}</strong></h2>";		
	}
	catch(Exception $e)
	{
		echo "<h2 style='color:red'>Error: ". $e->getMessage() . "</h2>";
	}
	echo "<hr/>";
}
else if(isset($_POST['fbgraph_author']) || isset($_POST['fbgraph_title']) || isset($_POST['fbgraph_description']))
	echo "<h2 style='color:red'>Error: Title, author, content must be provided and cannot be empty.</h2>";
?>
<style type='text/css'>
	form.customForm label{ display:block; }
</style>

<form class="customForm" action="<?php echo $_SERVER['REQUEST_URI']; ?>" id="genericImportFormId" method="post">
	<label for="fbgraph_author">Insert post author's ID:</label>
	<input type="text" size="20" maxlength="250" onclick="select();" value="" tabindex="1" noBanglaText="noBanglaText" name="fbgraph_author" style="display:inline" />
	
	<a href='https://developers.facebook.com/tools/explorer/145634995501895' target='_blank'>ID finder-[FB]</a>
	<a href='https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get' target='_blank'>ID finder-[G+]</a>
	
	<label for="fbgraph_author">Insert post author's Name: <small>(optional, will be used if specified userid does not exist)</small></label>
	<input type="text" size="20" maxlength="250" onclick="select();" tabindex="2" id="fbgraph_author_name" value="" name="fbgraph_author_name" style="display:inline" />

	<label for="authorCreateCommand" style="display:block">Author: </label>
	<select name="authorCreateCommand">
		<option value="authorCreateCommand_normal" selected>Create if not exist</option>
		<option value="authorCreateCommand_skipNewPost">Only create</option>
	</select>
	
	<label for="provider" style="display:block">Select provider</label>
	<select name="provider">
		<option value="google">Google</option>
		<option value="facebook" selected>Facebook</option>
		<option value="unknonwn">Unknown</option>
	</select> 
	
	<label for="fbgraph_title">Post title</label>
	<input type="text" maxlength="1000" size="50" value="" name="fbgraph_title" id="fbgraph_title" />
	
	<label for="fbgraph_description">Post content</label>
	<textarea id="fbgraph_description" name="fbgraph_description" rows="10" cols="100"></textarea>
	<br />
	
	<br />
	<br/>
	<input class="button button-primary" type="submit" name="submit" value="Save value" style="display:inline" />
</form>

<script type="text/javascript" src="http://bangla-keyboard-layout.googlecode.com/svn/trunk/rokeya_layout-4.4.73.js"></script>
<script type="text/javascript">
window.onload = function () {
	new banglaLayout("fbgraph_description").loadHelpTooltip();
	new banglaLayout("fbgraph_title").loadHelpTooltip();
	new banglaLayout("fbgraph_author_name").loadHelpTooltip();
};

document.getElementById("genericImportFormId").onsubmit = function(){
	var n = this.fbgraph_author.value;
	if(!isNaN(parseFloat(n)) && isFinite(n))
	{
		//It's numeric
		if (confirm("Regular userids should not be numeric. Do you want to convert this userid as regular userid?\n\nOK= Convert as regular userid. \nCancel= Do not convert to regular userid"))
			this.fbgraph_author.value += '	';
	}
	return true;
}
</script>
<?php
}
?>